Invoking the C Preprocessor

Contents|Index|Previous
Invoking the C preprocessor

Most often when you use the C preprocessor you will not have to invoke it explicitly: the C compiler will do so automatically. However, the preprocessor is sometimes useful on its own.

The C preprocessor expects two file names as arguments, infile and outfile. The preprocessor reads infile together with any other files it specifies with #include. All the output generated by the combined input files is written in outfile.

Either infile or outfile may be -, which as infile means to read from standard input and as outfile means to write to standard output. Also, if outfile or both file names are omitted, the standard output and standard input are used for the omitted file names.

What follows is a list of command options accepted by the C preprocessor. (These options can also be given when compiling a C program; they are passed along automatically to the preprocessor when it is invoked by the compiler.)

-P

Inhibit generation of # -lines with line-number information in the output from the preprocessor (see C preprocessor output). This might be useful when running the preprocessor on something that is not C code and will be sent to a program which might be confused by the #-lines.

-C

Do not discard comments: pass them through to the output file. Comments appearing in arguments of a macro call will be copied to the output before the expansion of the macro call.

-traditional

Try to imitate the behavior of old-fashioned C, as opposed to ANSI C.

- Traditional macro expansion pays no attention to single-quote or doublequote characters; macro argument symbols are replaced by the argument values even when they appear within apparent string or character constants.
- Traditionally, it is permissible for a macro expansion to end in the middle of a string or character constant. The constant continues into the text surrounding the macro call.
- However, traditionally the end of the line terminates a string or character constant, with no error.
- In traditional C, a comment is equivalent to no text at all. (In ANSI C, a comment counts as whitespace.)
- Traditional C does not have the concept of a “preprocessing number”. It considers 1.0e+4 to be three tokens: 1.0e, +, and 4.
- A macro is not suppressed within its own definition, in traditional C. Thus, any macro that is used recursively inevitably causes an error.
- The # character has no special meaning within a macro definition in traditional C.
- In traditional C, the text at the end of a macro expansion can run together with the text after the macro call, to produce a single token. (This is impossible in ANSI C.)
- Traditionally, \ inside a macro argument suppresses the syntactic significance of the following character.

-trigraphs

Process ANSI standard trigraph sequences. These are three-character sequences, all starting with ??, that are defined by ANSI C to stand for single characters. For example, ??/ stands for \, so ‘??/n’ is a character constant for a new-line. Strictly speaking, the GNU C preprocessor does not support all programs in ANSI C Standard unless -trigraphs is used, but if you ever notice the difference it will be with relief.
You don’t want to know any more about trigraphs.

-pedantic

Issue warnings required by the ANSI C standard in certain cases such as when text other than a comment follows #else or #endif.

-pedantic-errors

Like -pedantic, except that errors are produced rather than warnings.

-Wtrigraphs

Warn if any trigraphs are encountered (assuming they are enabled).

-Wcomment

Warn whenever a comment-start sequence /* appears in a comment.

-Wall

Requests both -Wtrigraphs and -Wcomment (but not -Wtraditional).

-Wtraditional

Warn about certain constructs that behave differently in traditional and ANSI C.

-I directory

Add the directory to the head of the list of directories to be searched for header files (see The #include directive). This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

-I-

Any directories specified with -I options before the -I- option are searched only for the case of #include "file"; they are not searched for #include <file>. If additional directories are specified with -I options after the -I-, these directories are searched for all #include directives. In addition, the -I- option inhibits the use of the current directory as the first search directory for #include " file". Therefore, the current directory is searched only if -I. is requested explicitly with it. Specifying both -I- and -I. allows you to control precisely which directories are searched before and which after the current one is searched.

-nostdinc

Do not search the standard system directories for header files. Only the directories you have specified with -I options (and the current directory, if appropriate) are searched.

-nostdinc++

Do not search for header files in the C++-specific standard directories, but do still search the other standard directories. (This option is used when building the C++ library.)

-D name

Predefine name as a macro, with definition 1.

-D name=definition

Predefine name as a macro, with definition definition. There are no restrictions on the contents of definition, but if you are invoking the preprocessor from a shell or shell-like program you may need to use the shell’s quoting syntax to protect characters such as spaces that have a meaning in the shell syntax. If you use more than one -D for the same name, the rightmost definition takes effect.

-U name

Do not predefine name. If both -U and -D are specified for one name, the -U beats the -D and the name is not predefined.

-undef

Do not predefine any nonstandard macros.

-A predicate(answer)

Make an assertion with the predicate and answer. See Assertions. You can use -A- to disable all predefined assertions; it also un-defines all predefined macros that identify the type of target system.

-dM

Instead of outputting the result of preprocessing, output a list of #define directives for all the macros defined during the execution of the preprocessor, including predefined macros. This gives you a way of finding out what is predefined in your version of the preprocessor; assuming you have no file foo.h, the following command will show the values of any predefined macros.

    touch foo.h; cpp -dM foo.h

-dD

Like -dM except in two respects: it does not include the pre-defined macros, and it outputs both the #define directives and the result of preprocessing. Both kinds of output go to the standard output file.

-M [-MG]

Instead of outputting the result of preprocessing, output a rule suitable for make describing the dependencies of the main source file. The preprocessor outputs one make rule containing the object file name for that source file, a colon, and the names of all the included files. If there are many included files then the rule is split into several lines using \-newline. -MG says to treat missing header files as generated files and assume they live in the same directory as the source file. It must be specified in addition to -M. This feature is used in automatic updating of makefiles.

-MM [-MG]

Like -M, mentions only the files included with #include"file". System header files included with #include<file> are omitted.

-MD file

Like -M but the dependency information is written to file. This is in addition to compiling the file as specified—-MD does not inhibit ordinary compilation the way -M does. When invoking gcc, do not specify the file argument. gcc will create file names made by replacing ".c" with ".d" at the end of the input file names.
In Mach, you can use the utility md to merge multiple dependency files into a single dependency file suitable for using with the make command.

-MMD file

Like -MD except mention only user header files, not system header files.

-H

Print the name of each header file used, in addition to other normal activities.

-imacros file

Process file as input, discarding the resulting output, before processing the regular input file. Because the output generated from file is discarded, the only effect of -imacros file is to make the macros defined in file available for use in the main input.

-include file

Process file as input, and include all the resulting output, before processing the regular input file.

-idirafter dir

Add the directory dir to the second include path. The directories on the second include path are searched when a header file is not found in any of the directories in the main include path (the one that -I adds to).

-iprefix prefix

Specify prefix as a prefix for using subsequent -iwithprefix options.

-iwithprefix dir

Add a directory to the second include path. The directory’s name is made by concatenating prefix and dir, where prefix was previously specified with -iprefix.

-isystem dir

Add a directory to the beginning of the second include path, marking it as a system directory, so that it gets the same special treatment as is applied to the standard system directories.

-lang-c
-lang-c89
-lang-c++
-lang-objc
-lang-objc++

Specify the source language. -lang-c is the default; it allows recognition of C++ comments (comments that begin with // and end at end of line), since this is a common feature and it will most likely be in the next C standard. -lang-c89 disables recognition of C++ comments. -lang-c++ handles C++ comment syntax and includes extra default include directories for C++. -lang-objc enables the Objective C #import directive. -lang-objc++ enables both C++ and Objective C extensions. These options are generated by the compiler driver gcc, but not passed from the gcc command line unless you use the driver’s -Wp option.

-lint

Look for commands to the program checker lint embedded in comments, and emit them preceded by #pragma lint.
For example, /* NOTREACHED */ becomes #pragma lint NOTREACHED.
This option is available only when you call cpp directly; gcc will not pass it from its command line.

-$

Forbid the use of $ in identifiers. This is required for ANSI conformance. gcc automatically supplies this option to the preprocessor if you specify -ansi, but gcc doesn’t recognize the -$ option itself—to use it without the other effects of -ansi, you must call the preprocessor directly.

Top|Contents|Index|Previous